<script src="/bootstrap/js/bootstrap.min.js"></script>
<script src="/js/jquery.cookie.js"></script>
<div id="page">
	<% if(@selfuser) %>
	<div class="mod-note-button">
		<input type="button" id="btnAllDoc" class="btn btn-mini btn-info" value="≡ 所有文档（a）" onclick="location.href='/doc/';" />
		<input type="button" class="btn btn-mini" value="← 返回上页（b）" onclick="__back();" /> 

		<% if(@lastdocs != nil && @lastdocs.length > 0) %>

			<div class="btn-group">
                <button id="btnLaster" class="btn btn-mini dropdown-toggle" data-toggle="dropdown">≈ 最近文档（z） <span class="caret"></span></button>
                <ul class="dropdown-menu">
                	<% @lastdocs.each do|doc| %>
                  	<li><a href="<%=getDetailUrl(doc)%>"><%=doc.title%></a></li>
                  	<% end %>
                </ul>
              </div>
        <% end %>

		<button class="btn btn-mini" type="button" onclick="__new();">♀ 新建文档（n）</button>
		<button class="btn btn-mini btn-primary" type="button" onclick="__edit();">∷ 编辑（e）</button>
		<button class="btn btn-mini" type="button" onclick="__delete()">× 删除（d）</button>
		<button class="btn btn-mini<%if(@doc[:shareid] == 1)%> btn-success <%end%>" type="button" onclick="__share()">分享</button>
		<input type="button" class="btn btn-mini btn-info" value="便笺" onclick="location.href='/memo/';" />
		<input type="button" id="btnFeedback" class="btn btn-mini btn-info" value="反馈" onclick="location.href='/feedback/';" />

	</div>
	<% end %>
	<div class="mod-note-title">
		<h1><%=@doc.title%></h1>
		<div class="desc">正文字数：<%=@doc.wordcount%> &nbsp;&nbsp;&nbsp;&nbsp;
		更新日期：<%=@doc.updated_at.in_time_zone(+8.hours).strftime('%Y-%m-%d') %> 
	</div>
	</div>
	<div class="content">
		<%=raw @doc.content%>
	</div>
</div>

<script type="text/javascript">

$().ready(function(){

	// 定义快捷键
	var keyCodes = { 'code.65': 'a', 'code.66': 'b', 'code.68':'d', 'code.69': 'e', 'code.78': 'n', 'code.90': 'z' };
	var hotKeys = {
		'a': function(){ $('#btnAllDoc').click(); },
		'b': __back,
		'n': __new,
		'e': __edit,
		'z': function(){ $('#btnLaster').click(); },
		'd': __delete
	};

	// 绑定快捷键
	$(document).bind('keyup', function(evt){
		var letter = keyCodes['code.' + evt.keyCode];
		if(letter != null) {
			var fun = hotKeys[letter];
			if(fun != null) fun();
		}
	});

	$.ajaxSetup({  
	    beforeSend: function(xhr) {  
	        xhr.setRequestHeader("X-CSRF-Token", $("meta[name=\"csrf-token\"]").attr("content"));  
	    }  
	});

	<% if(@selfuser) %>
	// 写入cookie
	addCookie('<%=@doc.id%>');
	<% end %>
});

// 分享
function __share(){
	var shareid = '<%=@doc.shareid%>';
	if(shareid == 1) {
		alert('该文档已设置为公开状态：\n您可以拷贝该页面的 url 地址，发送给其他人查看。');
	} else {
		alert('该文档没有设置为公开状态，不能分享给其他人查看。\n您可以点击【编辑】，设置为其他人可见。');
	}
}

function addCookie(docid){
	try {
		docid = docid.toString();
		if(docid == '') return;

		var ids = $.cookie('last_docids');
		if(ids == null) {
			ids = docid;
		} else {
			var arr = ids.split('|');
			var news = [ docid ];
			for(i in arr) {
				if(arr[i] != docid) {
					news.push(arr[i]);
				}
			}
			ids = news.slice(0, 6).join('|');
		}
		$.cookie('last_docids', ids);
	} catch(e){}
}


function __back(){
	if(location.hash.indexOf('#') != -1) {
			history.go(-2);
		} else {
			history.back();
		}
 }
function __new(){ location.href = '/doc/new/'; }
function __edit(){ location.href = '/doc/edit/<%=@doc.id%>'; }
function __delete() {
	docid = '<%=@doc.id%>';
	if(confirm("确认要删除该篇文档吗?\r\r删除后不可恢复。")) {
		$.post('/doc/delete', { 'docid':docid }, function(data){
			if(data == 1) {
				alert('删除成功');
				location.href = '/doc';
				return;
			}
			alert('发生意外');
		});
	}
}

</script>


